// i.d(t, { b: () => AttachmentsData })
import Data from "../object/Data"
import ObservableMap, { createMap } from "../observer/observable.map"
import funcUtil from "../utils/func.util"
import assetTypeEnum from "../enum/assetType.enum"
import Attachment from "../object/Attachment"
export default class AttachmentsData extends Data {
  mattertagAttachments: ObservableMap<Attachment>
  pendingAttachments: ObservableMap<Attachment>
  removedAttachments: ObservableMap<Attachment>
  constructor() {
    super()
    this.name = "media-attachments-data"
    this.mattertagAttachments = createMap()
    this.pendingAttachments = createMap()
    this.removedAttachments = createMap()
  }
  iteratePending(e: (t: Attachment) => void) {
    for (const t of this.pendingAttachments) e(t)
  }
  iterateRemoved(e: (t: Attachment) => void) {
    for (const t of this.removedAttachments) e(t)
  }
  iterateTagAttachments(e: (t: Attachment) => void) {
    for (const t of this.mattertagAttachments) e(t)
  }
  get pendings() {
    return this.pendingAttachments
  }
  get removals() {
    return this.removedAttachments
  }
  get tagAttachments() {
    return this.mattertagAttachments
  }
  getTagAttachment(e: string) {
    return this.mattertagAttachments.get(e)
  }
  getPendingAttachment(e: string) {
    return this.pendingAttachments.get(e)
  }
  getRemovedAttachment(e: string) {
    return this.removedAttachments.get(e)
  }
  addPendingAttachment(e) {
    this.pendingAttachments.set(e.id, e)
  }
  getTagAttachments(assetId: string) {
    const t: Attachment[] = []
    this.iterateTagAttachments(i => {
      i.assetType === assetTypeEnum.MATTERTAG && i.assetId === assetId && t.push(i)
    })
    return t
  }
  getPendingAttachmentsForAsset(e, t) {
    const i = []
    this.iteratePending(n => {
      n.assetType === t && n.assetId === e && i.push(n)
    })
    return i
  }
  getRemovedAttachmentsForAsset(e, t) {
    const i = []
    this.iterateRemoved(n => {
      n.assetType === t && n.assetId === e && i.push(n)
    })
    return i
  }
  markAttachmentForDelete(e) {
    const t = new Attachment(e)
    this.removedAttachments.set(t.id, t)
  }
  clearRemovedAttachments() {
    this.removedAttachments.clear()
  }
  removePendingAttachment(e: string) {
    this.pendingAttachments.has(e) && (this.pendingAttachments.delete(e), this.commit())
  }
  clearPendingAttachments() {
    this.pendingAttachments.clear()
  }
  addTagAttachment(e: Attachment) {
    this.mattertagAttachments.has(e.id) ? this.mattertagAttachments.get(e.id).copy(e) : this.mattertagAttachments.set(e.id, e)
  }
  getTempAttachmentId() {
    let e = funcUtil.randomStr(11)
    for (; this.getPendingAttachment(e) || this.getTagAttachment(e) || this.getRemovedAttachment(e); ) e = funcUtil.randomStr(11)
    return e
  }
}
